package com.example.springboot.controller;

import com.example.springboot.dao.UserMapper;
import com.example.springboot.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//@RestController
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserMapper userMapper;

    @RequestMapping("list") //
    @ResponseBody
    public List<User> list(){
        List<User> list = userMapper.selectUser();
        return list;
    }

    @RequestMapping("/listHtml")
    public String listHtml(ModelMap map){
        List<User> list = userMapper.selectUser();
        map.put("userList",list);
        return "/user/userlist";
    }

    @RequestMapping("add")
    public String add(){
        return "/user/add";
    }

    @RequestMapping("save")
    @ResponseBody
    public String save(User user){
        user.setId(1);
        userMapper.save(user);
        return "保存成功";
    }

    @RequestMapping("update") //
    @ResponseBody
    public String update(){
        User user = new User(3,"王五",23);
        userMapper.update(user);
        return "修改成功";
    }


    @RequestMapping("delete") //
    @ResponseBody
    public String delete(){
        userMapper.delete(3);
        return "删除成功";
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable("id") int id){
        userMapper.delete(id);
        return "删除成功";
    }
}